/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean.session;

import entity.RawMaterialQuotationEntity;
import entity.ReceiptEntity;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import type.RawMaterialQuotationType;

/**
 *
 * @author wei
 */
@Stateless
public class RawMaterialQuotationFacade extends AbstractFacade<RawMaterialQuotationEntity> {

    @PersistenceContext(unitName = "MacTeam-warPU")
    private EntityManager em;
    @Resource
    SessionContext ctx;

    protected EntityManager getEntityManager() {
        return em;
    }

    public RawMaterialQuotationFacade() {
        super(RawMaterialQuotationEntity.class);
    }

    public List<RawMaterialQuotationEntity> getQuotedQuotation() {


        String queryStr = "Select r from RawMaterialQuotation r"
                + " where r.Type=:type";
        Query query = em.createQuery(queryStr);
        query.setParameter("type", RawMaterialQuotationType.quoted);
        return query.getResultList();
    }

    public List<RawMaterialQuotationEntity> getUnquotedQuotation() {


        String queryStr = "Select r from RawMaterialQuotation r"
                + " where r.Type=:type";
        Query query = em.createQuery(queryStr);
        query.setParameter("type", RawMaterialQuotationType.notquoted);
        return query.getResultList();
    }

    public List<RawMaterialQuotationEntity> getInvalidQuotation(Date d1) {
        List<RawMaterialQuotationEntity> result = new ArrayList<RawMaterialQuotationEntity>();

        List<RawMaterialQuotationEntity> result1 = getUnquotedQuotation();
        for (RawMaterialQuotationEntity q : result1) {
            Date d = q.getValidity();
            if (d==null||d.before(d1)) {
                result.add(q);
            }
        }
        return result;
    }

    public boolean checkAllQuotationsSameCategoryHasBeenQuoted(RawMaterialQuotationEntity r) {

        List<RawMaterialQuotationEntity> result = new ArrayList<RawMaterialQuotationEntity>();
        String rName = r.getRawMaterial().getProductName();
        Date rValidity = r.getValidity();
        List<RawMaterialQuotationEntity> result1 = getUnquotedQuotation();
        for (RawMaterialQuotationEntity q : result1) {
            Date d = q.getValidity();
            String pName = q.getRawMaterial().getProductName();

            //if there r 1 unquoted same category ,return false;
            if (d.equals(rValidity) && pName.equalsIgnoreCase(rName)) {
                return false;
            }
        }
        return true;
    }

    public void scheduleTimer(Date validity, long id) {
        ctx.getTimerService().createTimer(validity, id);
    }

    @Timeout
    public void timeoutHandler(Timer timer) {
        RawMaterialQuotationEntity s = em.find(RawMaterialQuotationEntity.class, timer.getInfo());
        if (!(s.getType().equals(equals(RawMaterialQuotationType.accepted)))) {
            s.setType(RawMaterialQuotationType.rejected);
            em.merge(s);
        }
        timer.cancel();
    }
}
